define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

	const controller = {
		index() {
			Table.api.init({
				extend: {
					index_url: 'business/privatesea/index',
					add_url: 'business/privatesea/add',
					edit_url: 'business/privatesea/edit',
					del_url: 'business/privatesea/del',
					recovery_url: 'business/privatesea/recovery',
					table: 'business',
				},
			});

			const table = $('#table');

			table.bootstrapTable({
				// 请求地址
				url: $.fn.bootstrapTable.defaults.extend.index_url,
				// 设置工具栏元素
				toolbar: '.toolbar',
				// 设置主键
				pk: 'id',
				// 排序字段
				sortName: 'id',
				// 默认排序方式
				sortOrder: 'desc',
				showExport: false,
				// 渲染表格数据
				columns: [
					{ checkbox: true },
					{ field: 'id', title: 'ID', operate: false },
					{ field: 'nickname', title: __('Bnickname'), operate: 'LIKE' },
					{ field: 'mobile', title: __('mobile'), operate: 'LIKE' },
					{ field: 'email', title: __('email'), operate: 'LIKE' },
					{ field: 'money', title: __('money'), formatter: (v) => '￥' + v, operate: 'LIKE' },
					{ field: 'source.name', title: __('Bsource') },
					{ field: 'gender', title: __('Gender'), formatter: (v) => ['保密', '男', '女'][v] },
					{
						field: 'deal',
						title: __('BdealText'),
						searchList: { 0: __('未成交'), 1: __('已成交') },
						formatter: Table.api.formatter.normal,
					},
					{
						field: 'auth',
						title: __('BAuthStatus'),
						searchList: { 0: __('未认证'), 1: __('已认证') },
						formatter: Table.api.formatter.normal,
					},
					{ field: 'admin.username', title: __('Ausername'), operate: false, operate: 'LIKE' },
					{
						field: 'operate',
						title: __('Operate'),
						table: table,
						events: Table.api.events.operate,
						formatter: Table.api.formatter.operate,
						buttons: [
							{
								name: 'business',
								title: '客户详情',
								extend: 'data-toggle="tooltip"',
								classname: 'btn btn-xs btn-primary btn-dialog vivst_test',
								icon: 'fa fa-eye',
								url: 'business/privateseainfo/index?ids={id}',
							},
							{
								name: 'recovery',
								confirm: '确定回收？',
								title: '回收该客户',
								extend: 'data-toggle="tooltip"',
								icon: 'fa fa-recycle',
								classname: 'btn btn-xs btn-success btn-ajax',
								url: 'business/privatesea/recovery?ids={id}',
								success: function (data, ret) {
									console.log(data, ret);
									// 刷新表格
									table.bootstrapTable('refresh');
								},
								error: function (err) {
									console.log(err);
								},
							},
						],
					},
				],
			});

			// 回收
      $('.btn-recovery').click(function () {
        const ids = Table.api.selectedids(table).toString();
        layer.confirm('确定回收？', {
          title: '提示',
          btn: ['确定', '取消'],
        }, index => { 
          // 发送请求
          $.post($.fn.bootstrapTable.defaults.extend.recovery_url, { ids: ids }, function (res) {
            if (res.code == 1) {
              Toastr.success(res.msg);
              // 刷新表格
              table.bootstrapTable('refresh');
            } else {
              Toastr.error(res.msg);
            }
          }, 'json')

          layer.close(index);
        })
      })

			Table.api.bindevent(table);
		},
		add() {
			// 选中地区事件处理
			$('#region').on('cp:updated', function () {
				const citypicker = $(this).data('citypicker');
				const code =
					citypicker.getCode('district') || citypicker.getCode('city') || citypicker.getCode('province');
				$('#region-code').val(code);
			});

			controller.api.bindevent();
		},
		edit() {
			$('#region').on('cp:updated', function () {
				var citypicker = $(this).data('citypicker');
				var code =
					citypicker.getCode('district') || citypicker.getCode('city') || citypicker.getCode('province');
				$('#region-code').val(code);
			});

			controller.api.bindevent();
		},
		del() {
			controller.api.bindevent();
		},
		api: {
			// JS模块化的全局方法
			bindevent: function () {
				Form.api.bindevent($('form[role=form]'));
			},
		},
	};

	return controller;
});
